package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.AccountDto;
import com.qf.commerce.common.dto.AccountDto2;
import com.qf.commerce.common.vo.AccountVo;
import com.qf.commerce.entity.Account;
import com.qf.commerce.mapper.AccountMapper;
import com.qf.commerce.service.AccountService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountMapper accountMapper;


    @Override
    public int add(AccountDto accountDto) {
        int i = 0;
        i = accountMapper.insertAccount(accountDto);
        if (i <= 0) {
            throw new RuntimeException("添加失败");
        }
        return i;
    }

    @Override
    public int delete(List<Long> ids) {
        int i = 0;
         i = accountMapper.deleteAccount(ids);
        if (i <= 0) {
            throw new RuntimeException("删除失败");
        }
        return i;
    }

    @Override
    public int updateAccount(Long accountId,Account account) {
        int i = accountMapper.updateAccount(accountId,account);
        if (i <= 0) {
            throw new RuntimeException("修改失败");
        }
        return i;
    }


    @Override
    public int updateRole(Long organizationId, Long roleId, Long accountId) {
        return accountMapper.updateRole(organizationId,roleId,accountId);
    }


    @Override
    public int updatePassword(String accountPassword, Long accountId) {
        int i = accountMapper.updatePassword(accountPassword, accountId);
        if (i <= 0) {
            throw new RuntimeException("修改失败");
        }
        return i;
    }


    @Override
    public List<AccountDto> selectAccountDetail(Long accountId) {
        return accountMapper.selectAccountDetail(accountId);
    }
    @Override
    public PageInfo<AccountVo> selectAccount(String organizationName, Integer status, String accountName, String userName, String roleName,Integer page, Integer size) {
        return CommonBeanUtils.copyPage(PageHelper.startPage(page,size).doSelectPageInfo(() -> accountMapper.selectAccount(organizationName,status,accountName,userName,roleName)),new PageInfo<AccountVo>(),AccountVo::new);
    }




    //个人信息模块
    @Override
    public List<AccountDto2> selectPersonDetailById(Long accountId) {
        return accountMapper.selectPersonDetailById(accountId);
    }
    @Override
    public int updatePersonDetailById(Long accountId, Account account) {
        return accountMapper.updatePersonDetailById(accountId,account);
    }


}
